package com.example.reptile.article;


import com.example.reptile.config.ChromeConfig;
import com.example.reptile.entity.PersonEntity;
import com.example.reptile.utils.ExcelUtils;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "重点人网页爬虫")
@RestController
@RequestMapping("/keyPerson")
@Log4j2
public class SeleniumTest {

    public static void main(String[] args) {
        getKeyPersonInfo();
    }

    @ApiOperation("爬取重点人列表信息")
    @GetMapping("/getKeyPersonInfo")
    public static String getKeyPersonInfo() {
        WebDriver driver = new ChromeDriver(ChromeConfig.getOptions());
        List result = new ArrayList();
        try {
            driver.manage().window().maximize();
            driver.get("http://10.168.4.197:10001/#/login");
            TimeUnit.SECONDS.sleep(5);
            driver.findElement(By.className("logo")).click();
            driver.findElements(By.tagName("input")).get(0).sendKeys("111111");
            driver.findElements(By.tagName("input")).get(1).sendKeys("123456");
            driver.findElement(By.tagName("button")).click();
            TimeUnit.SECONDS.sleep(5);
            driver.findElement(By.className("home")).click();
            TimeUnit.SECONDS.sleep(8);
            WebElement keyPersonElement = driver.findElement(By.className("person-list"));
            TimeUnit.SECONDS.sleep(5);
            result = parseElement(keyPersonElement);
            ExcelUtils.exportData(result, PersonEntity.class);
        } catch (InterruptedException e) {
            log.error("爬取过程中断", e);
        } finally {
            driver.quit();
        }
        return new Gson().toJson(result);
    }


    private static List parseElement(WebElement content) {
        List list = new ArrayList();
        List<WebElement> lis = content.findElements(By.tagName("li"));
        for (WebElement li : lis) {
            List<WebElement> divs = li.findElements(By.tagName("div"));
            WebElement td = divs.get(1);
            String url = td.findElement(By.tagName("img")).getAttribute("src");
            WebElement infos = divs.get(2);
            List<WebElement> ps = infos.findElements(By.tagName("p"));
            List<WebElement> spans = ps.get(0).findElements(By.tagName("span"));
            String name = spans.get(0).getText();
            String sex = spans.get(1).getText();
            String age = spans.get(2).getText();
            String phone = spans.get(3).getText();
            PersonEntity entity = new PersonEntity();
            entity.setUrl(url).setName(name).setSex(sex).setAge(age).setPhone(phone);
            list.add(entity);
        }
        return list;
    }


}